//
//  JHView.m
//  练习轮播图
//
//  Created by 苗钧豪 on 16/7/21.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "JHView.h"
@interface JHView ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIPageControl *page;
@end
@implementation JHView
#pragma mark ================ 协议方法 ================
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 改变pagecontrol的位置
    NSInteger num = scrollView.contentOffset.x / self.bounds.size.width;
    _page.currentPage = num - 1;
    if (num == 0) {
        // 改变滚动视图的偏移量
        _scrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imageArr.count - 2), 0);
        _page.currentPage = _page.numberOfPages - 1;
    }
    else if (num == _imageArr.count - 1) {
        _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _page.currentPage = 0;
    }
}
#pragma mark ================ 初始化 ================
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor yellowColor];
        // 子控件初始化时，frame不要与初始化的方法一样
        // 推荐使用self.bounds / CGRectMake(0,0,frame.size.width,frame.size.height)
        [self createscroll];
        [self createpage:frame];
    }
    return self;
}
- (void)createscroll{
    _scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
    _scrollView.delegate = self;
    _scrollView.backgroundColor = [UIColor colorWithRed:0.014 green:0.000 blue:0.000 alpha:1.000];
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_scrollView];

}
// 后期需要添加page的位置改变，这里默认位置
- (void)createpage:(CGRect)frame{
    _page = [[UIPageControl alloc]initWithFrame:(CGRectMake(100, 100, frame.size.width / 3, 30))];
    _page.numberOfPages = 5;
//    _page.backgroundColor = [UIColor greenColor];
    _page.center = CGPointMake(frame.size.width / 2, frame.size.height - 15);
    _page.pageIndicatorTintColor = [UIColor redColor];
    [_page addTarget:self action:@selector(action:) forControlEvents:(UIControlEventValueChanged)];
    [self addSubview:_page];
}
// 问题：如何通过pagecontrol实现轮播效果？
- (void)action:(UIPageControl *)page
{
    [_scrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
}
#pragma mark ================ imageArr ================
// 自定义方法
- (void)setimagesWithArr:(nonnull NSArray *)array
{
    // 根据传过来的数组进行处理，生成图片数组
    NSArray *imagesArr = [self handleArray:array];
    // 逻辑判断，要用到数组，写成属性
    self.imageArr = imagesArr;
    // 根据这个数组生成对应的imageview
    [self createAllImageViewWithImageArray:imagesArr];
    // 根据图片数组，设置对应的视图
    _page.numberOfPages = array.count;
    _scrollView.contentSize = CGSizeMake(imagesArr.count * self.bounds.size.width, 0);
    // 因为添加了最后一张图，要让轮播图看起来在第一张
    _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
}
// 处理数组
- (NSArray *)handleArray:(NSArray *)array
{
    // 判断类型,不知道别人使用你的类的时候传进来的类型是什么
    id firstObject = [array firstObject];
    // 判断对象是不是某种类型 isKindofClass
    // 最终的图片数组
    NSMutableArray *imagesArr = [NSMutableArray array];
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把 字符串 转换成 图片 数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imagesArr addObject:image];
        }
    }
    else if ([firstObject isKindOfClass:[UIImage class]]) {
        [imagesArr setArray:array];
    }
    // 获取第一张图
    UIImage *firstimage = [imagesArr firstObject];
    // 获取到最后一张图
    UIImage *lastimage = [imagesArr lastObject];
    // 下标为0的位置放最后一张图
    [imagesArr insertObject:lastimage atIndex:0];
    // 在最后加第一张图
    [imagesArr addObject:firstimage];

    return [imagesArr copy];
}
// 遍历创建UIImageview
- (void)createAllImageViewWithImageArray:(NSArray *)imagesArr
{
    // 使用数组创建UIImageView
    for (int i = 0; i < imagesArr.count ; i ++) {
        UIImageView *imgview = [[UIImageView alloc]initWithFrame:(CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height))];
        imgview.image = imagesArr[i];
        [_scrollView addSubview:imgview];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
